package com.hunliji.monitor.web.strategy.alert;

import com.hunliji.common.exception.WarnException;
import com.hunliji.monitor.share.enums.AlertActionTypeEnum;
import java.util.HashMap;
import java.util.Map;

public class AlertActionContext {

    // 存放所有策略类Bean的map
    private static Map<String, AlertAction> alertActionStrategyBeanMap =
            new HashMap<>();

    public static AlertAction getMerchantIndexStrategy(AlertActionTypeEnum alertActionTypeEnum) {
        String strategy = getStrategy(alertActionTypeEnum);
        AlertAction merchantIndex = alertActionStrategyBeanMap.get(strategy);
        if (merchantIndex == null) {
            throw new WarnException("没有对应的处理器");
        }
        return merchantIndex;
    }

    public static void setMerchantIndexStrategy(AlertActionTypeEnum alertActionTypeEnum, AlertAction alertAction) {
        if (alertActionTypeEnum == null) {
            return;
        }
        alertActionStrategyBeanMap.put(getStrategy(alertActionTypeEnum), alertAction);
    }

    /**
     * 获取策略
     *
     * @param alertActionTypeEnum
     * @return
     */
    private static String getStrategy(AlertActionTypeEnum alertActionTypeEnum) {
        return alertActionTypeEnum.getCode();
    }
}
